package com.zzjc.web.util;

import com.zzjc.util.UUIDUtils;

import java.io.*;
import java.util.Base64;


public class Base64Util {
	
	private final Base64.Decoder decoder = Base64.getDecoder();
	private final Base64.Encoder encoder = Base64.getEncoder();
	
	private String ext;
	private String oldFileName;
	private String newFileName;
	private String resourcePath;
	private String fullPath;
	private String urlPath;
	private File   file;
	
	private final String projectStaticPath ="/var/www/html/doorway/rec/doorwayImages";
	private final String servicePath ="/doorway/rec/doorwayImages";
//	private final String projectStaticPath = "D:/doorwayImages";
	public static Base64Util build() {
		return new Base64Util();
	}
	
	public static Base64Util build(String oldFileName,String ext) {
		return new Base64Util(oldFileName,ext);
	}
	
	public Base64Util() {
		
	}
	
	public Base64Util(String oldFileName,String ext) {
		this.ext = ext;
		this.oldFileName = oldFileName;
	}

	public String gencodeImageStr(String imgFile) {
		InputStream inputStream = null;
	    byte[] data = null;
	    try {
	        inputStream = new FileInputStream(imgFile);
	        data = new byte[inputStream.available()];
	        inputStream.read(data);
	        inputStream.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return encoder.encodeToString(data);
	}
	
	public String generateImage(String base64Image) {
		base64Image= base64Image.replaceAll(" ","+");

		byte[] imageByte = decoder.decode(base64Image);
		for(int i = 0;i<imageByte.length;++i) {
			if(imageByte[i]<0) {
				imageByte[i] += 256;
			}
		}
		this.createDir();
		try {
			this.createFile();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		this.write(this.file, imageByte);
		return newFileName;
	}
	
	private String createDir(){
		String pathPre = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
		File dir=new File(this.projectStaticPath);
		if(!dir.exists()) {
			dir.mkdir();
		}
		String dirPath = this.projectStaticPath +"/"+pathPre;
		/**
		 * linux路径
		 */
        String urlPath=this.servicePath+"/"+pathPre;
		/**
		 * windows路径
		 */
//		String urlPath=pathPre;
        this.fullPath = dirPath;
        this.urlPath=urlPath;
        this.resourcePath = pathPre;
		dir=new File(dirPath);
        if(!dir.exists()) {
        	dir.mkdir();
        }
        return dirPath;
    }

	private void createFile() throws IOException{
		//生成一个新的has文件名
		String uuid = UUIDUtils.getUUID();
		String fileNamePre = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
		this.newFileName = fileNamePre+uuid.substring(0,16)+"."+ this.ext;
		String newFileName = this.fullPath+"/"+this.newFileName;
        File file=new File(newFileName);
        if(!file.exists()) {
        	file.createNewFile();
        }else {
        	createFile();
        }
        this.file = file;
    }
	
	private void write(File file,byte[] content) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);
			os.write(content, 0, content.length);
			os.flush(); 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public String getExt() {
		return ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}
	
	public String getOldFileName() {
		return oldFileName;
	}

	public void setOldFileName(String oldFileName) {
		this.oldFileName = oldFileName;
	}

	public String getNewFileName() {
		return newFileName;
	}

	public void setNewFileName(String newFileName) {
		this.newFileName = newFileName;
	}
	
	public String getResourcePath() {
		return resourcePath;
	}

	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	public String getUrlPath() {
		return urlPath;
	}

	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
	/**
	 * 用例说明
	 * @param args
	 */
	public static void main(String[] args) {
		String base64Image = Base64Util.build().gencodeImageStr("C:\\Users\\Lenovo\\Desktop\\u=4259300811,497831842&fm=26&gp=0.jpg");
		System.out.println(base64Image);
		Base64Util base64Util = Base64Util.build();
		base64Util.generateImage(base64Image);
	}
}
